package com.xinxin.leetcode.problem496;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

/**
 * @author ShiXinXin
 * @date 2020-01-06 15:29
 */
public class Solution {
    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        int[] result = new int[nums1.length];
        Stack<Integer> stack = new Stack<>();
        Map<Integer, Integer> map = new HashMap<>(100);
        for (int num : nums2) {
            while (!stack.empty() && num > stack.peek()) {
                map.put(stack.pop(), num);
            }
            stack.push(num);
        }
        for (int i = 0; i < nums1.length; i++) {
            Integer res = map.get(nums1[i]);
            result[i] = res == null ? -1 : res;
        }
        return result;
    }

    public static void main(String[] args) {
        int[] num1 = {1, 3, 5, 2, 4}, num2 = {6, 5, 4, 3, 2, 1, 7};
        System.out.println(Arrays.toString(new Solution().nextGreaterElement(num1, num2)));
    }
}
